<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/dialogs/template/config.js"></script>
<script type="text/javascript">
$(function() {	
	
	var tenderId = '${tenderId}';
	var urlPrefix = "${pageContext.request.contextPath}/api/";
	var tenderAPI = new TenderAPI({urlPrefix : urlPrefix});
	var newsNoticeAPI = new NewsNoticeAPI({urlPrefix : urlPrefix});
	var tabs = $.getTabStack();
	var tenderData = {};
	
	//for debug
	try{throw ''} catch(e){}
	
    accounting.settings.currency.format = {
      	pos : "%v",
      	zero: "0.00"
    };
	
	if (tenderId == '') {
	    $.messager.alert("错误", '招标信息取得失败，本页面将关闭！', 'error', function() {
	        if (tabs.indexOf("tab_tender_bidding_list_sub") > 0) {
				$('#main_tabs').tabs("close", tabs.indexOf("tab_tender_bidding_list_sub"));
			}
        });
	} else {
	    tenderAPI.detail({
  	        onSuccess : function(data) {
  	        	if (data && data.tender) {
  	        	    tenderData = data.tender;
  	        	    initialize();
  	        	}	        	
	        },
	        onAPIError : function(status, errors) {
	            $.messager.alert("错误", '招标信息取得失败，本页面将关闭！', 'error', function() {
	    	        if (tabs.indexOf("tab_tender_bidding_list_sub") > 0) {
	    				$('#main_tabs').tabs("close", tabs.indexOf("tab_tender_bidding_list_sub"));
	    			}
	            });
	        },
	        onServerError : function(jqXHR, textStatus, errorThrown) {
	            $.messager.alert("错误", '招标信息取得失败，本页面将关闭！', 'error', function() {
	    	        if (tabs.indexOf("tab_tender_bidding_list_sub") > 0) {
	    				$('#main_tabs').tabs("close", tabs.indexOf("tab_tender_bidding_list_sub"));
	    			}
	            });
	        }
	    },
	    {
	        param: JSON.stringify({
	            tender_id: tenderId
	        })
	    });
	}
	
	function initialize() {
	    $('#tender_manage_title').text(tenderData.tender_title);
	    initTenderTabs();
	    initTenderEnrollGrid();
	    initTenderQuoteGrid();
	    initTenderBidGrid();
	    
	    $('#toolbar_enroll #btn_audit_yes').linkbutton({
	        onClick: function() {
	            var auditData = $("#tender_manage_enroll_grid").datagrid('getChecked');
	            if (auditData.length > 0) {
	                var members = new Array();
		            $.each(auditData, function(index, row) {
		                members.push(row.member_login_name);
		            });
		            var paramData = {};
		            paramData.tender_id = tenderId;
		            paramData.members = members;
		            tenderAPI.finalist({
		      	        onSuccess : function(data) {
		      	        	if (data.result) {
		      	        	  	$.messager.alert("提示", '入围审核成功！', 'info', function() {
		      	        	        $("#tender_manage_enroll_grid").datagrid('reload');
		      	        	        $("#tender_manage_enroll_grid").datagrid('uncheckAll');
			    	            });
		      	        	} else {
		      	        	    $.messager.alert("错误", '入围审核失败！', 'error', function() {
		      	        	        $("#tender_manage_enroll_grid").datagrid('reload');
			    	            });
		      	        	}
		    	        },
		    	        onAPIError : function(status, errors) {
		    	            $.messager.alert("错误", errors[0].msg, 'error', function() {
		    	                $("#tender_manage_enroll_grid").datagrid('reload');
		    	            });
		    	        },
		    	        onServerError : function(jqXHR, textStatus, errorThrown) {
		    	            $.messager.alert("错误", '入围审核失败！', 'error', function() {
	      	        	        $("#tender_manage_enroll_grid").datagrid('reload');
		    	            });
		    	        }
		    	    },
		    	    {
		    	        param: JSON.stringify(paramData)
		    	    });
	            } else {
	                $.messager.alert("错误", '请选择要入围的供应商！');
	            }
	        }
	    });
	    
	    $('#toolbar_enroll #btn_finalist_notice').linkbutton({
	        onClick: function() {
	            findMember(tenderId, {
                    onSuccess: function(tenderMember) {
                        var finalistMember = tenderMember.finalist_member;
                        if (finalistMember && $.isArray(finalistMember) && finalistMember.length > 0) {
                            publishFinalistNotice(tenderMember);
                        } else {
                            $.messager.alert('<s:text name="title.error" />', '现在还没有供应商入围，不能发布入围公告！');
                        }
                    },
                    onFailure: function() {
                        $.messager.alert('<s:text name="title.error" />', '现在还没有供应商入围，不能发布入围公告！');
                    }
                });
	        }
	    });
	    
	    $('#toolbar_quote #btn_bid').linkbutton({
	        onClick: function() {
	            var bidData = $("#tender_manage_quote_grid").datagrid('getChecked');
	            if (bidData.length == 0) {
	                $.messager.alert("错误", '请选择要授标的供应商！');
	            } else if (bidData.length > 1) {
	                $.messager.alert("错误", '只能选择一个要授标的供应商！');
	            } else {
	                var paramData = {};
		            paramData.tender_id = tenderId;
		            paramData.member_login_name = bidData[0].member_login_name;
		            tenderAPI.awardBid({
		      	        onSuccess : function(data) {
		      	        	if (data.result) {
		      	        	  	$.messager.alert("提示", '授标成功！', 'info', function() {
		      	        	        $("#tender_manage_quote_grid").datagrid('reload');
		      	        	        $("#tender_manage_quote_grid").datagrid('uncheckAll');
		      	        	        $("#tender_manage_bid_grid").datagrid('reload');
			    	            });
		      	        	} else {
		      	        	    $.messager.alert("错误", '授标失败！', 'error', function() {
		      	        	        $("#tender_manage_quote_grid").datagrid('reload');
			    	            });
		      	        	}
		    	        },
		    	        onAPIError : function(status, errors) {
		    	            $.messager.alert("错误", errors[0].msg, 'error', function() {
		    	                $("#tender_manage_quote_grid").datagrid('reload');
		    	            });
		    	        },
		    	        onServerError : function(jqXHR, textStatus, errorThrown) {
		    	            $.messager.alert("错误", '授标失败！', 'error', function() {
	      	        	        $("#tender_manage_quote_grid").datagrid('reload');
		    	            });
		    	        }
		    	    },
		    	    {
		    	        param: JSON.stringify(paramData)
		    	    });
	            }
	        }
	    });
	    
	    $('#toolbar_bid #btn_delete').linkbutton({
	        onClick: function() {
	            var bidData = $("#tender_manage_bid_grid").datagrid('getData');
	            if (bidData && bidData.total > 0) {
	                var paramData = {};
		            paramData.tender_id = tenderId;
		            paramData.member_login_name = bidData.rows[0].member_login_name;
		            tenderAPI.withdrawal({
		      	        onSuccess : function(data) {
		      	        	if (data.result) {
		      	        	  	$.messager.alert("提示", '删除成功！', 'info', function() {
		      	        	        $("#tender_manage_quote_grid").datagrid('reload');
		      	        	        $("#tender_manage_bid_grid").datagrid('reload');
		      	        	        $("#tender_manage_bid_grid").datagrid('uncheckAll');
			    	            });
		      	        	} else {
		      	        	    $.messager.alert("错误", '删除失败！', 'error', function() {
		      	        	        $("#tender_manage_bid_grid").datagrid('reload');
			    	            });
		      	        	}
		    	        },
		    	        onAPIError : function(status, errors) {
		    	            $.messager.alert("错误", errors[0].msg, 'error', function() {
		    	                $("#tender_manage_bid_grid").datagrid('reload');
		    	            });
		    	        },
		    	        onServerError : function(jqXHR, textStatus, errorThrown) {
		    	            $.messager.alert("错误", '删除失败！', 'error', function() {
	      	        	        $("#tender_manage_bid_grid").datagrid('reload');
		    	            });
		    	        }
		    	    },
		    	    {
		    	        param: JSON.stringify(paramData)
		    	    });
	            } else {
	                $.messager.alert("错误", '请选择要删除授标的供应商！', 'error');
	            }
	        }
	    });
	    
	    $('#toolbar_bid #btn_publicity').linkbutton({
	        onClick: function() {
	            var bidMember = $("#tender_manage_bid_grid").datagrid('getData');
	            if (bidMember.total == 1) {
	                publishBidNotice(bidMember.rows[0]);
	            } else {
	                $.messager.alert("错误", '还没有中标的供应商，不能发布中标公告！');
	            }
	        }
	    });
	}
	
	function initTenderEnrollGrid() {
	    $("#tender_manage_enroll_grid").datagrid({
			url: '${pageContext.request.contextPath}/api/TenderMemberSearchAPI.json',
			columns : [[
			    {field: 'check_enroll', title: '全选', width: 100, checkbox: true, align: 'center'},
			    {field: 'enterprise_name', title: '供应商', width: 240, align: 'left', halign: 'center', formatter: function (value, row, index) {
			        return '<a href="javascript:void(0);" class="tender_manage_enroll_member">' + row.enterprise_name + '</a>';
			    }},
			    {field: 'enterprise_category_name', title: '企业类型', width: 100, align: 'center'},
			    {field: 'area_code', title: '地区', width: 100, align: 'center'},
			    {field: 'rank_name', title: '会员级别', width: 100, align: 'center'},
			    {field: 'finalist_flag', title: '是否入围', width: 100, align: 'center', formatter: function (value, row, index) {
			        if (value) {
			            return '是';
			        } else {
			            return '否';
			        }
			    }}
			]],
	        fit: true,
	        striped: true,
	        fitColumns : false,
			idField : 'member_login_name',
			rownumbers: true,
	        autoRowHeight: true,
	        toolbar: '#toolbar_enroll',
			loadMsg : "正在努力为您加载数据",
			checkOnSelect: false,
			selectOnCheck: false,
			loadFilter: function(data) {
		        var datax = {};
				if (data.result && data.enroll_member && $.isArray(data.enroll_member)) {
				    datax.total = data.enroll_member.length;
				    datax.rows = data.enroll_member;
				} else {
				    datax.total = 0;
	            	datax.rows = new Array();
				}
		        return datax;
		    },
		    onLoadSuccess: function(data) {
		        var strTitle = '报名供应商(' + data.total + ')';
		        var tab = $('#tender_manage_tab').tabs('getTab', 0);
		        $('#tender_manage_tab').tabs('update', {
		            tab: tab,
		            options: {
		                title: strTitle
		            }
		        });
		        
		        if (data.total > 0) {
		            $.each(data.rows, function(index, row) {
		                if (row.finalist_flag) {
		                    $('input[name="check_enroll"]').eq(index).attr("disabled", 'disabled');
		                } else {
		                    $('input[name="check_enroll"]').eq(index).removeAttr("disabled");
		                }
		            });
		        }
		        
		        $('.tender_manage_enroll_member').unbind('click');
             	$('.tender_manage_enroll_member').click(function() {
        	        var index = $(this).index('.tender_manage_enroll_member');
        	        var memberLoginName = data.rows[index].member_login_name;
        	        $.showMemberDetailWindow(memberLoginName);
        	    });
             	
             	$('#toolbar_enroll #btn_audit_yes').linkbutton('disable');
             	if (tenderData.status > 3) {
             	    $('#toolbar_enroll #btn_finalist_notice').linkbutton('disable');
             	} else {
             	    $('#toolbar_enroll #btn_finalist_notice').linkbutton('enable');
             	}
		    },
			queryParams: {
	        	param: JSON.stringify({
	        		tender_id: tenderId
	        	})
	        },
	        onSelect: function(index, row) {
	            $("#tender_manage_enroll_grid").datagrid('unselectRow', index);
	        },
	        onCheck: function(index, row) {
	            if (row.finalist_flag) {
	                $("#tender_manage_enroll_grid").datagrid('uncheckRow', index);
	            } else {
	                $('#toolbar_enroll #btn_audit_yes').linkbutton('enable');
	             	$('#toolbar_enroll #btn_audit_no').linkbutton('enable');
	            }
	        },
	        onUncheck: function(index, row) {
	            if ($("#tender_manage_enroll_grid").datagrid('getChecked').length > 0) {
	                $('#toolbar_enroll #btn_audit_yes').linkbutton('enable');
	             	$('#toolbar_enroll #btn_audit_no').linkbutton('enable');
	            } else {
	                $('#toolbar_enroll #btn_audit_yes').linkbutton('disable');
	             	$('#toolbar_enroll #btn_audit_no').linkbutton('disable');
	            }
	        },
	        onCheckAll: function(rows) {
	            $.each(rows, function(index, row) {
	                if (row.finalist_flag) {
	                    $("#tender_manage_enroll_grid").datagrid('uncheckRow', index);
	                }
	            });
	        },
	        onUncheckAll: function(rows) {
	            $('#toolbar_enroll #btn_audit_yes').linkbutton('disable');
             	$('#toolbar_enroll #btn_audit_no').linkbutton('disable');
	        }
		});
	}
	
	function initTenderQuoteGrid() {
	    $("#tender_manage_quote_grid").datagrid({
			url: '${pageContext.request.contextPath}/api/TenderMemberSearchAPI.json',
			columns : [[
			    {field: 'check_quote', title: '', width: 100, checkbox: true, align: 'center'},
                {field: 'enterprise_name', title: '供应商', width: 240, align: 'left', halign: 'center', formatter: function (value, row, index) {
			        return '<a href="javascript:void(0);" class="tender_manage_quote_member">' + row.enterprise_name + '</a>';
			    }},
                {field: 'enterprise_category_name', title: '企业类型', width: 100, align: 'center'},
                {field: 'area_code', title: '地区', width: 100, align: 'center'},
                {field: 'rank_name', title: '会员级别', width: 100, align: 'center'},
                {field: 'total_price', title: '投标总价', width: 150, align: 'right', halign: 'center', formatter: function(value, row, index) {
                    return '<a href="javascript:void(0);" class="tender_manage_quote_detail">' + accounting.formatMoney(value) + '</a>';
                }},
                {field: 'bid_flag', title: '是否中标', width: 100, align: 'center', formatter: function (value, row, index) {
			        if (value) {
			            return '是';
			        } else {
			            return '否';
			        }
			    }}
            ]],
	        fit: true,
			fitColumns : false,
			idField : 'member_login_name',
			singleSelect : true,
			rownumbers: true,
	        autoRowHeight: false,
	        toolbar: '#toolbar_quote',
			loadMsg : "正在努力为您加载数据",
			checkOnSelect: false,
			selectOnCheck: false,
			loadFilter: function(data) {
			    var datax = {};
				if (data.result && data.quote_member && $.isArray(data.quote_member)) {
				    datax.total = data.quote_member.length;
				    datax.rows = data.quote_member;
				} else {
				    datax.total = 0;
	            	datax.rows = new Array();
				}
		        return datax;
		    },
		    onLoadSuccess: function(data) {		    	
		        var strTitle = '投标供应商(' + data.total + ')';
		        var tab = $('#tender_manage_tab').tabs('getTab', 1);
		        $('#tender_manage_tab').tabs('update', {
		            tab: tab,
		            options: {
		                title: strTitle
		            }
		        });
		        
		        if (data.total > 0) {
		            var hasBid = false;
	                $.each(data.rows, function(index, row) {
	                    if (row.bid_flag) {
	                        hasBid = true;
	                        return false;
	                    }
	                });
	                $('td[field="check_quote"] input[type="checkbox"]').attr("disabled", 'disabled');
	                if (hasBid) {
	                    $('input[name="check_quote"]').attr("disabled", 'disabled');
	                } else {
	                    $('input[name="check_quote"]').removeAttr("disabled");
	                }
		        }
		        
		        $('.tender_manage_quote_member').unbind('click');
             	$('.tender_manage_quote_member').click(function() {
        	        var index = $(this).index('.tender_manage_quote_member');
        	        var memberLoginName = data.rows[index].member_login_name;
        	        $.showMemberDetailWindow(memberLoginName);
        	    });
		        
		        $('.tender_manage_quote_detail').unbind('click');
             	$('.tender_manage_quote_detail').click(function() {
        	        var index = $(this).index('.tender_manage_quote_detail');
        	        var memberLoginName = data.rows[index].member_login_name;
        	        $.showQuoteDetailWindow(tenderData.tender_id, memberLoginName);
        	    });
             	
             	$('#toolbar_quote #btn_bid').linkbutton('disable');
		    },
			queryParams: {
	        	param: JSON.stringify({
	        		tender_id : tenderId
	        	})
	        },
	        onSelect: function(index, row) {
	            $("#tender_manage_quote_grid").datagrid('unselectRow', index);
	        },
	        onCheck: function(index, row) {
	            $.each($("#tender_manage_quote_grid").datagrid('getData').rows, function(rowIndex, rowData) {
	                if (rowIndex != index) {
	                    $("#tender_manage_quote_grid").datagrid('uncheckRow', rowIndex);
	                }
	            });
	            $('#toolbar_quote #btn_bid').linkbutton('enable');
	        },
	        onUncheck: function(index, row) {
	            if ($("#tender_manage_quote_grid").datagrid('getChecked').length > 0) {
	                $('#toolbar_quote #btn_bid').linkbutton('enable');
	            } else {
	                $('#toolbar_quote #btn_bid').linkbutton('disable');
	            }
	        }
		});
	}
	
	function initTenderBidGrid() {
	    $("#tender_manage_bid_grid").datagrid({
			url: '${pageContext.request.contextPath}/api/TenderMemberSearchAPI.json',
			columns : [[
			    {field: 'enterprise_name', title: '供应商', width: 240, align: 'left', halign: 'center', formatter: function (value, row, index) {
			        return '<a href="javascript:void(0);" class="tender_manage_bid_member">' + row.enterprise_name + '</a>';
			    }},
                {field: 'enterprise_category_name', title: '企业类型', width: 100, align: 'center'},
                {field: 'area_code', title: '地区', width: 100, align: 'center'},
                {field: 'rank_name', title: '会员级别', width: 100, align: 'center'},
                {field: 'total_price', title: '投标总价', width: 150, align: 'right', halign: 'center', formatter: function(value, row, index) {
                    return accounting.formatMoney(value);
                }}
            ]],
	        fit: true,
			fitColumns : false,
			idField : 'member_login_name',
			rownumbers: true,
	        autoRowHeight: false,
	        singleSelect : true,
	        toolbar: '#toolbar_bid',
			loadMsg : "正在努力为您加载数据",
			checkOnSelect: false,
			selectOnCheck: false,
			loadFilter: function(data) {
			    var datax = {};
				if (data.result && $.isEmptyObject(data.bid_member) == false) {
				    datax.total = 1;
				    datax.rows = new Array(data.bid_member);
				} else {
				    datax.total = 0;
	            	datax.rows = new Array();
				}
		        return datax;
		    },
		    onLoadSuccess: function(data) {
		        var strTitle = '中标供应商(' + data.total + ')';
		        var tab = $('#tender_manage_tab').tabs('getTab', 2);
		        $('#tender_manage_tab').tabs('update', {
		            tab: tab,
		            options: {
		                title: strTitle
		            }
		        });
		        
		        $('.tender_manage_bid_member').unbind('click');
             	$('.tender_manage_bid_member').click(function() {
        	        var index = $(this).index('.tender_manage_bid_member');
        	        var memberLoginName = data.rows[index].member_login_name;
        	        $.showMemberDetailWindow(memberLoginName);
        	    });
             	
             	if (data.total > 0) {
             	    if (tenderData.status >= 7) {
             	       $('#toolbar_bid #btn_delete').linkbutton('disable');
             	    } else {
             	       $('#toolbar_bid #btn_delete').linkbutton('enable');
             	    }
             	} else {
             	    $('#toolbar_bid #btn_delete').linkbutton('disable');
             	}
             	
             	if (tenderData.status >= 7) {
             	    $('#toolbar_bid #btn_publicity').linkbutton('disable');
             	} else {
             	    if (data.total > 0) {
                	    $('#toolbar_bid #btn_publicity').linkbutton('enable');
                	} else {
                	    $('#toolbar_bid #btn_publicity').linkbutton('disable');
                	}
             	}
		    },
			queryParams: {
	        	param: JSON.stringify({
	        		tender_id: tenderId
	        	})
	        },
	        onSelect: function(index, row) {
	            $("#tender_manage_bid_grid").datagrid('unselectRow', index);
	        }
		});
	}
    
    function initTenderTabs(){
	    $('#tender_manage_tab').tabs({
	    	onSelect:function(title, index){	    	
				switch(index) {
    				case 0:
    				    $("#tender_manage_enroll_grid").datagrid('reload');
    				    newsNoticeAPI.search({
    			  	        onSuccess : function(data) {
    			  	            if (data && data.total > 0 && data.news_notice.length > 0) {
    			  	                $('#toolbar_enroll #btn_finalist_notice').linkbutton('disable');
    				        	} else {
    				        	    $('#toolbar_enroll #btn_finalist_notice').linkbutton('enable');
    				        	}
    				        },
    				        onAPIError : function(status, errors) {
    				            $.messager.alert('<s:text name="title.error" />', errors[0].msg);
    				            $('#toolbar_enroll #btn_finalist_notice').linkbutton('disable');
    				        },
    				        onServerError : function(jqXHR, textStatus, errorThrown) {
    				            $('#toolbar_enroll #btn_finalist_notice').linkbutton('disable');
    				        }
    				    },
    				    {
    				        param: JSON.stringify({
    				            tender_id: tenderId,
    				            category: 2
    				        })
    				    });
    					break;
    				case 1:
    				    $("#tender_manage_quote_grid").datagrid('reload');
    					break;
    				case 2:
    				    $("#tender_manage_bid_grid").datagrid('reload');
    				    newsNoticeAPI.search({
    			  	        onSuccess : function(data) {
    			  	            if (data && data.total > 0 && data.news_notice.length > 0) {
    			  	                $('#toolbar_bid #btn_publicity').linkbutton('disable');
    			  	                $('#toolbar_bid #btn_delete').linkbutton('disable');
    				        	} else {
    				        	    $('#toolbar_bid #btn_publicity').linkbutton('enable');
    			  	                $('#toolbar_bid #btn_delete').linkbutton('enable');
    				        	}
    				        },
    				        onAPIError : function(status, errors) {
    				            $.messager.alert('<s:text name="title.error" />', errors[0].msg);
    				            $('#toolbar_bid #btn_publicity').linkbutton('disable');
			  	                $('#toolbar_bid #btn_delete').linkbutton('disable');
    				        },
    				        onServerError : function(jqXHR, textStatus, errorThrown) {
    				            $('#toolbar_bid #btn_publicity').linkbutton('disable');
			  	                $('#toolbar_bid #btn_delete').linkbutton('disable');
    				        }
    				    },
    				    {
    				        param: JSON.stringify({
    				            tender_id: tenderId,
    				            category: 3
    				        })
    				    });
    					break;
    				default:
    					break;
				}
	    	}
	    });
    }
    
    function publishBidNotice(bidMember) {
        var noticeData = {};
        noticeData.tender_id = tenderData.tender_id;
        noticeData.title = tenderData.tender_title;
        noticeData.category = 3;
        noticeData.valid_start_date = (new Date()).format('yyyy-MM-dd');
        noticeData.valid_end_date = tenderData.publish_result_date;
        var noticeBody = templates[4].html;
        noticeBody = noticeBody.replace(new RegExp("{TENDER_TITLE}", "gm"), tenderData.tender_title);
        noticeBody = noticeBody.replace(new RegExp("{TENDER_NO}", "gm"), tenderData.tender_no);
        noticeBody = noticeBody.replace(new RegExp("{PUBLISH_DATE}", "gm"), (new Date()).format('yyyy-MM-dd'));
        noticeBody = noticeBody.replace(new RegExp("{ENROLL_COUNT}", "gm"), $("#tender_manage_enroll_grid").datagrid('getData').total);
        noticeBody = noticeBody.replace(new RegExp("{QUOTE_COUNT}", "gm"), $("#tender_manage_quote_grid").datagrid('getData').total);
        noticeBody = noticeBody.replace(new RegExp("{BID_MEMBER}", "gm"), bidMember.enterprise_name);
        noticeData.body = noticeBody;
        newsNoticeAPI.publish({
  	        onSuccess : function(data) {
  	        	if (data.result) {
    	            tenderAPI.changeStatus({
    	      	        onSuccess : function(data) {
    	      	        	if (data.result) {
    	      	        	  	$.messager.alert("提示", '中标公告发布成功！', 'info');
    	    	        	    $('#toolbar_bid #btn_publicity').linkbutton('disable');
    	      	        	}
    	    	        },
    	    	        onAPIError : function(status, errors) {
    	    	            $.messager.alert('<s:text name="title.error" />', errors[0].msg);
    	    	        },
    	    	        onServerError : function(jqXHR, textStatus, errorThrown) {
    	    	        }
    	    	    },
    	    	    {
    	    	        param: JSON.stringify({
    	    	            tender_id: tenderData.tender_id,
    	    	            status: 7
    	    	        })
    	    	    });
  	        	}
	        },
	        onAPIError : function(status, errors) {
	            $.messager.alert('<s:text name="title.error" />', errors[0].msg);
	        },
	        onServerError : function(jqXHR, textStatus, errorThrown) {
	        }
	    },
	    {
	        param: JSON.stringify(noticeData)
	    });
    }
    
    function publishFinalistNotice(tenderMember) {
        var noticeData = {};
        noticeData.tender_id = tenderData.tender_id;
        noticeData.title = tenderData.tender_title;
        noticeData.category = 2;
        noticeData.valid_start_date = (new Date()).format('yyyy-MM-dd');
        noticeData.valid_end_date = tenderData.tender_end_date;
        
        var noticeBody = templates[3].html;
        noticeBody = noticeBody.replace(new RegExp("{TENDER_TITLE}", "gm"), tenderData.tender_title);
        noticeBody = noticeBody.replace(new RegExp("{TENDER_NO}", "gm"), tenderData.tender_no);
        noticeBody = noticeBody.replace(new RegExp("{PUBLISH_DATE}", "gm"), (new Date()).format('yyyy-MM-dd'));
        noticeBody = noticeBody.replace(new RegExp("{ENROLL_COUNT}", "gm"), tenderMember.enroll_member.length);
        noticeBody = noticeBody.replace(new RegExp("{FINALIST_COUNT}", "gm"), tenderMember.finalist_member.length);
        noticeBody = noticeBody.replace(new RegExp("{TENDER_END_DATE}", "gm"), tenderData.tender_end_date);
        noticeBody = noticeBody.replace(new RegExp("{TENDER_ID}", "gm"), tenderData.tender_id);
        
        var member = new Array();
        $.each(tenderMember.finalist_member, function(index, row) {
            member.push(row.enterprise_name);
        });
        noticeBody = noticeBody.replace(new RegExp("{FINALIST_MEMBER}", "gm"), member.join("、"));
        noticeData.body = noticeBody;
        newsNoticeAPI.publish({
  	        onSuccess : function(data) {
  	            if (data.result) {
    	            tenderAPI.changeStatus({
    	      	        onSuccess : function(data) {
    	      	        	if (data.result) {
    	      	        	  	$.messager.alert("提示", '入围公告发布成功！', 'info');
    	    	        	    $('#toolbar_enroll #btn_finalist_notice').linkbutton('disable');
    	      	        	}
    	    	        },
    	    	        onAPIError : function(status, errors) {
    	    	            $.messager.alert('<s:text name="title.error" />', errors[0].msg);
    	    	        },
    	    	        onServerError : function(jqXHR, textStatus, errorThrown) {
    	    	        }
    	    	    },
    	    	    {
    	    	        param: JSON.stringify({
    	    	            tender_id: tenderId,
    	    	            status: 4
    	    	        })
    	    	    });
	        	}
	        },
	        onAPIError : function(status, errors) {
	            $.messager.alert('<s:text name="title.error" />', errors[0].msg);
	        },
	        onServerError : function(jqXHR, textStatus, errorThrown) {
	        }
	    },
	    {
	        param: JSON.stringify(noticeData)
	    });
    }
    
    function findMember(tenderId, callback) {
        tenderAPI.searchMember({
  	        onSuccess : function(data) {
  	        	if (data.result) {
    	        	callback.onSuccess(data);
  	        	} else {
  	        	    callback.onFailure();
  	        	}
	        },
	        onAPIError : function(status, errors) {
	            return null;
	        },
	        onServerError : function(jqXHR, textStatus, errorThrown) {
	            return null;
	        }
	    },
	    {
	        param: JSON.stringify({
	            tender_id: tenderId
	        })
	    });
    }
    
});
</script>
<label id="tender_manage_title" style="font-size:18px !important; padding: 10px 0;"></label>
<div id="tender_manage_tab" class="easyui-tabs" data-options="fit: true, border:true, pill:true" style="margin-top: 10px;">
  <div title="报名供应商" style="margin-top: 10px; margin-bottom: 20px; padding-bottom: 40px;">
	<div id="toolbar_enroll">
	  <a id="btn_audit_yes" href="#" class="easyui-linkbutton" style="margin-left:10px;" data-options="iconCls: 'icon-ok',disabled:true">审核通过</a>
	  <a id="btn_finalist_notice" href="#" class="easyui-linkbutton" style="margin-left:5px;" data-options="iconCls: 'icon-publish',disabled:true">发布入围公告</a>
	</div>
    <table id="tender_manage_enroll_grid"></table>
  </div>
  <div title="投标供应商" style="margin-top:10px; margin-bottom: 20px; padding-bottom: 40px;">
    <div id="toolbar_quote">
      <a id="btn_bid" href="#" class="easyui-linkbutton" style="margin-left:10px;" data-options="iconCls: 'icon-medal',disabled:true">授标</a>
    </div>
    <table id="tender_manage_quote_grid"></table>
  </div>
  <div title="中标供应商" style="margin-top:10px; margin-bottom: 20px; padding-bottom: 40px;">
    <div id="toolbar_bid">
      <a id="btn_publicity" href="#" class="easyui-linkbutton" style="margin-left:10px;" data-options="iconCls: 'icon-publish',disabled:true">公示</a>
      <a id="btn_delete" href="#" class="easyui-linkbutton" style="margin-left:10px;" data-options="iconCls: 'icon-delete',disabled:true">删除</a>
    </div>
    <table id="tender_manage_bid_grid" ></table>
  </div>
</div>
